package com.ctgu.AutoIdentify.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctgu.AutoIdentify.DTO.UserDTO;
import com.ctgu.AutoIdentify.common.Constants;
import com.ctgu.AutoIdentify.common.Result;
import com.ctgu.AutoIdentify.entity.User;
import com.ctgu.AutoIdentify.service.IUserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>

 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @CrossOrigin(origins = "*")
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){

        String username = userDTO.getUsername();
        String password = userDTO.getPassword();

        if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            return Result.error(Constants.CODE_400, "参数错误");//前端发送来的数据不可信，因此还需在后端校验一次
        }
        UserDTO dto = userService.login(userDTO);
        return Result.success(dto);
    }

    @CrossOrigin(origins = "*")
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        if(StringUtils.isBlank(username)){
            return Result.error(Constants.CODE_400, "参数错误");
        }
        return Result.success(userService.register(userDTO));
    }

    @CrossOrigin(origins = "*")
    @GetMapping("/username/{username}")
    public Result findByUsername(@PathVariable String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return Result.success(userService.getOne(queryWrapper));
    }

    @CrossOrigin(origins = "*")
    @PostMapping
    public Result save(@RequestBody User user){
        //新增或者更新
        return Result.success(userService.saveOrUpdate(user));
    }
    @PostMapping("/password")   //    /user/password
    public Result password(@RequestBody User user) {
        userService.updatePassword(user);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
    }



    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(userService.removeByIds(ids));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(userService.list());
    }

    @GetMapping("/{id}")//根据id获取一整条用户数据
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String username
                          ) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(username)) {
            queryWrapper.like("username", username);
        }

        return Result.success(userService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


}

